package com.schoolcardinfo.biz.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.schoolcardinfo.beans.SchoolId;
import com.schoolcardinfo.biz.ISchoolIdBiz;
import com.schoolcardinfo.dao.DBHelper;

public class SchoolIdBizImpl implements ISchoolIdBiz{
	private DBHelper db=new DBHelper();
	@Override
	public int addSchoolId(int studentId) {
		String sql="insert into schoolId values(null,?,default,0)";
		List<Object> params=new ArrayList<Object>();
		params.add(studentId);
		int result=db.update(sql, params);
		return result;
	}

	@Override
	public List<Map<String,Object>> selectAllSchoolCard() {
		String sql="";
		List<Map<String,Object>> list = null;
		sql="select * from schoolId c,id_studentInfo s where c.studentId=s.studentId";
		list=db.find(sql, null);
		if(list.size()>0){
			return list;
		}else{
			return null;
		}
	}

	@Override
	public int updateCardstatus(int status1, int status2,int studentId) {
		String sql="update schoolId set status=? where studentId=? and status=?";
		List<Object> params=new ArrayList<Object>();
		params.add(status2);
		params.add(studentId);
		params.add(status1);
		int result=db.update(sql, params);
		return result;
	}

	@Override
	public int updateCardMoney(double money,int sid) {
		String sql = "";
		List<Object> params=new ArrayList<Object>();
		if(money != 0.0) {
			sql = "update schoolId set balance=balance+? where sid=?";
			params.add(money);
		} else {
			sql = "update schoolId set balance=0 where sid=?";
		}
		params.add(sid);
		int result=db.update(sql, params);
		return result;
	}

	@Override
	public List<Map<String, Object>> selectSidSchoolCard(int sid) {
		String sql="";
		List<Map<String,Object>> list = null;
		sql="select * from schoolId c,id_studentInfo s where c.studentId=s.studentId and c.sid="+sid;
		System.out.println(sid);
		list=db.find(sql, null);
		System.out.println(list);
		if(list.size()>0 || list!=null){
			return list;
		}else{
			return null;
		}
	}
	
	@Override
	public List<Map<String, Object>> selectStuIdSchoolCard(int studentId) {
		String sql="";
		List<Map<String,Object>> list = null;
		sql="select * from schoolId c,id_studentInfo s where c.studentId=s.studentId and c.studentId="+studentId;
		list=db.find(sql, null);
		if(list.size()>0){
			return list;
		}else{
			return null;
		}
	}

	@Override
	public List<Map<String, Object>> selectStatusSchoolCard(int status) {
		String sql="";
		List<Map<String,Object>> list = null;
		sql="select * from schoolId c,id_studentInfo s where c.studentId=s.studentId and status="+status;
		list=db.find(sql, null);
		if(list.size()>0){
			return list;
		}else{
			return null;
		}
	}

	@Override
	public int findStuInfo(int studentId) {
		String sql="select * from schoolId where studentId="+studentId;
		List<Map<String,Object>> list=db.find(sql, null);
		if(list.size()>0){
			return 1;
		}else{
			return 0;
		}
	}

	@Override
	public int updateLoseStatus(String studentId) {
		String sql="UPDATE schoolId SET status = 1 WHERE studentId in ('"+studentId+"') and status = 2";
		int result=db.update(sql, null);
		return result;
	}

	@Override
	public SchoolId findCardInfo(int studentId) {
		String sql = "select * from schoolId where studentId="+studentId+" and status=1";
		List<SchoolId> list = db.find(sql, null, SchoolId.class);
		if(list != null && list.size() > 0) {
			return (SchoolId) list.get(0);
		}
		return null;
	}
}
